<?php
/* $Id: types.group.php,JOP
/**
 * @package Issue-Tracker
 * @subpackage Groups
 */

if (strpos($_SERVER['PHP_SELF'],basename(__FILE__)) !== false) {
  print "Direct module access forbidden.";exit;}

if(is_admin())
{
  if ($_GET['subaction'] == "delete"
  and !empty($_GET['id'])) {
    if ($_POST['confirm'] == "true") {
        $sql  = "DELETE FROM z_group_types ";
        $sql .= "WHERE tid='".$_GET['id']."'";
        $dbi->query($sql);
        redirect("?module=groups&action=types");
    } else {
      $smarty->display("groups/types/delete.tpl");
    }
  } else if ($_GET['subaction'] == "new") {
    if ($_POST['commit'] == "true") {
      if (empty($_POST['text'])) {
        push_error("Text can not be empty.");
      } else {
        $sql  = "SELECT tid ";
        $sql .= "FROM z_group_types ";
        $sql .= "WHERE LOWER(text) = LOWER('".trim($_POST['text'])."')";
        $tid = $dbi->fetch_one($sql);
        if (!empty($tid)) {
          push_error(GMsg('00','8'));
        } else {

          $insert['text']       = $_POST['text'];
          $insert['customer']   = $_POST['customer'] == "on" ? "t" : "f";
          $insert['sub']        = $_POST['sub'] == "on" ? "t" : "f";
          $dbi->insert("z_group_types",$insert);
          redirect("?module=groups&action=types");
        }
      }
    }
    
    if (empty($_POST['commit']) or errors()) {
      $smarty->display("groups/types/new.tpl");
    }
  } else if ($_GET['subaction'] == "edit" and !empty($_GET['id'])) {
    if ($_POST['commit'] == "true") {
      if (empty($_POST['text'])) {
        push_error("Group Type can not be empty.");
      } else {
        $sql  = "SELECT tid ";
        $sql .= "FROM z_group_types ";
        $sql .= "WHERE LOWER(text) = LOWER('".trim($_POST['text'])."')";
        $tid = $dbi->fetch_one($sql);
        if (empty($tid) or $tid == $_GET['id']) {

          $update['text']       = $_POST['text'];
          $update['customer']   = $_POST['customer'] == "on" ? "t" : "f";
          $update['sub']        = $_POST['sub'] == "on" ? "t" : "f";
          $dbi->update("z_group_types",$update,"WHERE tid='".$_GET['id']."'");
          redirect("?module=groups&action=types");
        } else {
            push_error(GMsg('00','8'));
        }
      }
    } 

    if (empty($_POST['commit']) or errors()) 
	{
       $sql  = "SELECT * ";
       $sql .= "FROM z_group_types ";
       $sql .= "WHERE tid ='".$_GET['id']."'";
       $type = $dbi->fetch_row($sql,"array");
       $smarty->assign('type',$type);
       $smarty->display("groups/types/edit.tpl");
       }
  } else 
  {
    $links[] = array
	(
      "txt" => "New Type",
      "url" => "?module=groups&action=types&subaction=new",
      "img" => $_ENV['imgs']['user']
    );

    $sql  = "SELECT * ";
    $sql .= "FROM z_group_types ";
    $sql .= "ORDER BY tid";
    $t_datas = $dbi->fetch_all($sql,"array");
	
	//pour verser du t au Div check on
	if(is_array($t_datas))
	{
		foreach($t_datas as $key => $s_datas)
		{
			
			$t_datas[$key]['iedit'] = "?module=groups&action=types&subaction=edit&id=".$s_datas['tid'];
			$t_datas[$key]['idele'] = "?module=groups&action=types&subaction=delete&id=".$s_datas['tid'];
		}
	}

	$o_grid = new Grid("GGRPTYPE",Msg('00',2,$lg->type));
    $o_grid->setFieldCat(array('idkey','descr','fcust','fsubg','iedit','idele'));
	$o_grid->setDatas($t_datas);
	$o_grid->SetFieldAttr('*',"nowrap",true);
	$o_grid->SetFieldAttr('idkey',"width",3);
	$o_grid->SetFieldAttr('descr',"width",20);
	$o_grid->SetFieldAttr(array('fcust','fsubg','iedit','idele'),'width',5);

    $smarty->display("title_bar.tpl");
	echo $o_grid->Display();

  }
} else 
{
  access_denied();
}
?>